package com.soft.base.spring.request.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.soft.base.enums.ValueNameSerializer;
import com.soft.base.enums.ValueNameStringDeserializer;
import com.soft.base.spring.request.annotation.FieldFilterNullToZero;
import com.soft.base.spring.request.type.SortItemType;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
@Data
public class PaginateRequest<QO> {
    @ApiModelProperty("页码")
    @FieldFilterNullToZero
    @Min(value = 1, message = "页码最小为1")
    private Long page;
    @ApiModelProperty("分页大小(1-10000)")
    @FieldFilterNullToZero
    @Min(value = 1, message = "分页最小为1")
    @Max(value = 10000, message = "分页最大为10000")
    private Long pageSize;
    @ApiModelProperty("搜索")
    private QO search;
    @ApiModelProperty("排序")
    private SortItem order;

    public Boolean isSearchEmpty() {
        return null == search;
    }

    public Boolean isOrderEmpty() {
        return null == order || StrUtil.isEmpty(order.getField()) || null == order.getType();
    }

    @Data
    public static class SortItem {
        @ApiModelProperty("排序字段")
        private String field;
        @ApiModelProperty("排序方式")
        @JSONField(serializeUsing = ValueNameSerializer.class, deserializeUsing = ValueNameStringDeserializer.class)
        private SortItemType type;
    }
}
